package com.tang.javaSwingCaculate.Stack;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * @author tmh
 * @date 2022/12/13 19:39
 * @description 本类用于实现计算器的其他拓展功能
 */
@Slf4j
public class OtherFunction {

    /**
     * 求模运算
     *
     * @param input
     * @return 取模后的结果
     */
    public Double getMod(String input) {
        boolean contains = input.contains("-");
        if (contains) {
            String[] inputs = input.split("-");
            if (inputs.length > 1) {
                Double one = Double.parseDouble(inputs[0]);
                Double two = Double.parseDouble(inputs[1]);
                return one % two;
            }
        }
        return null;
    }

    /**
     * 求倒数运算
     *
     * @param input
     * @return 倒数后的结果
     */
    public Double getReciprocal(String input) {
        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        boolean b = pattern.matcher(input).matches();
        if (b) {
            log.info(String.valueOf(b));
            return 1 / Double.parseDouble(input);
        } else {
            return null;
        }
    }

    /**
     * 求平方的运算
     *
     * @param input
     * @return
     */
    public Double getSquare(String input) {

        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        boolean b = pattern.matcher(input).matches();
        if (b) {
            return Double.parseDouble(input) * Double.parseDouble(input);
        } else {
            return null;
        }
    }

    /**
     * 求根号的运算
     *
     * @param input
     * @return
     */
    public Double getRadical(String input) {
        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        if (pattern.matcher(input).matches()) {
            return Math.sqrt(Double.parseDouble(input));
        } else {
            return null;
        }
    }

    /**
     * 求出二进制
     *
     * @param input
     * @return
     */
    public Double getB(String input) {
        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        String result = "";
        boolean b = pattern.matcher(input).matches();
        if (!b) {
            return null;
        } else {
            // 要求二进制表示的小数
            double decimal = Double.parseDouble(input);
            MyStack myStackD = new MyStack(100);
            MyStack myStackP = new MyStack(100);
            if (decimal < 0) {
                decimal = Math.abs(decimal);
                // 用栈来保存二进制数
                int dec = (int) decimal;
                double part = decimal - dec;
                while (dec > 0) {
                    myStackD.push(String.valueOf(dec % 2));
                    dec = dec / 2;
                }
                while (part > 0) {
                    double r = part * 2;
                    if (r >= 1) {
                        myStackP.push("1");
                        part = r - 1;
                    } else {
                        myStackP.push("0");
                        part = r;
                    }
                }
                if (myStackP.length() == 0) {
                    String s = "-";
                    while (!myStackD.isMyStackEmpty()) {
                        s = s + myStackD.pop();
                    }
                    log.info(s);
                    result = s;
                } else {
                    String s = "-";
                    while (!myStackD.isMyStackEmpty()) {
                        s = s + myStackD.pop();
                    }
                    s = s + ".";
                    for (int i = 0; i < myStackP.length(); i++) {
                        s = s + myStackP.getStackArray()[i];
                    }
                    log.info(s);
                    result = s;
                }
            } else {
                int dec = (int) decimal;
                double part = decimal - dec;
                while (dec > 0) {
                    myStackD.push(String.valueOf(dec % 2));
                    dec = dec / 2;
                }

                while (part > 0) {
                    double r = part * 2;
                    if (r >= 1) {
                        myStackP.push("1");
                        part = r - 1;
                    } else {
                        myStackP.push("0");
                        part = r;
                    }
                }
                if (myStackP.length() == 0) {
                    String s = "";
                    while (!myStackD.isMyStackEmpty()) {
                        s = s + myStackD.pop();
                    }
                    log.info(s);
                    result = s;
                } else {
                    String s = "";
                    while (!myStackD.isMyStackEmpty()) {
                        s = s + myStackD.pop();
                    }
                    s = s + ".";
                    for (int i = 0; i < myStackP.length(); i++) {
                        s = s + myStackP.getStackArray()[i];
                    }
                    log.info(s);
                    result = s;
                }
            }
        }
        return Double.parseDouble(result);
    }

    /**
     * 求出改数的递归
     *
     * @param input
     * @return
     */
    public Double getFactorial(String input) {
        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        if (pattern.matcher(input).matches()) {
            double db = Double.parseDouble(input);
            if (db < 0) {
                db = -db;
                return -factorial(db);
            } else {
                return factorial(db);
            }
        } else {
            return null;
        }
    }

    public double factorial(double n) {
        if (n < 1) {
            return 1;
        }
        return n * factorial(n - 1);
    }

    /**
     * 获得PI值
     *
     * @return
     */
    public Double getP() {

        return Math.PI;
    }

    /**
     * 获得E值
     *
     * @return
     */
    public Double getE() {
        return Math.E;
    }

    /**
     * 获得一个数的绝对值
     *
     * @param input
     * @return
     */
    public Double getAbs(String input) {
        String regex = "^-?\\d+(\\.\\d+)?$";
        Pattern pattern = Pattern.compile(regex);
        if (pattern.matcher(input).matches()) {
            return Math.abs(Double.parseDouble(input));
        } else {
            return null;
        }
    }

}
